package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.enums.EnableStatusEnum;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证审核表 服务实现类
 * </p>
 *
 * @author wei
 * @since 2025-01-18
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {

    @Autowired
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;
    @Autowired
    private WorkerCertificationMapper workerCertificationMapper;

    @Override
    @Transactional
    public void certificationAudit (WorkerCertificationAuditAddReqDTO reqDTO) {
        Long l = UserContext.currentUserId ();
        if (l == null) {
            throw new RuntimeException ("用户未登录");
        }

        reqDTO.setServeProviderId (l);

        // TODO
        WorkerCertificationAudit workerCertificationAudit = BeanUtils.copyBean (reqDTO, WorkerCertificationAudit.class);
        WorkerCertification certification = BeanUtils.copyBean (reqDTO, WorkerCertification.class);
        certification.setId (l);
        certification.setCertificationStatus (CertificationStatusEnum.PROGRESSING.getStatus ());

        WorkerCertificationAudit workerCertification = workerCertificationAuditMapper.selectOne (Wrappers.<WorkerCertificationAudit>lambdaQuery ()
                .eq (WorkerCertificationAudit::getServeProviderId, l));
        if (workerCertification != null) {
            if (workerCertification.getCertificationStatus () ==CertificationStatusEnum.FAIL.getStatus ()) {
                int i = workerCertificationMapper.updateById (certification);
                if (i <= 0) {
                    throw new RuntimeException ("认证信息提交失败");
                }
                workerCertificationAudit.setId (workerCertification.getId ());
                workerCertificationAudit.setAuditStatus (EnableStatusEnum.DISABLE.getStatus ());
                workerCertificationAudit.setCertificationStatus (CertificationStatusEnum.PROGRESSING.getStatus ());
                boolean b = this.updateById (workerCertificationAudit);
                if (!b) {
                    throw new RuntimeException ("认证审核提交失败");
                }
            }
        }else {
            int insert = workerCertificationMapper.insert (certification);
            if (insert <= 0) {
                throw new RuntimeException ("认证信息提交失败");
            }

            boolean save = this.save (workerCertificationAudit);
            if (!save) {
                throw new RuntimeException ("认证审核提交失败");
            }
        }


    }

    /**
     * 运营端审核分页查询
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public PageResult<WorkerCertificationAuditResDTO> page (WorkerCertificationAuditPageQueryReqDTO dto) {
        Page<WorkerCertificationAudit> workerCertificationAuditPage = workerCertificationAuditMapper.selectPage (
                new Page<> (dto.getPageNo (), dto.getPageSize ()),
                Wrappers.<WorkerCertificationAudit>lambdaQuery ()
                        .eq (dto.getAuditStatus () != null, WorkerCertificationAudit::getAuditStatus, dto.getAuditStatus ())
                        .eq (dto.getCertificationStatus () != null, WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus ())
                        .like (dto.getIdCardNo () != null, WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo ())
                        .like (dto.getName () != null, WorkerCertificationAudit::getName, dto.getName ())
                        .orderBy (dto.getOrderBy1 () != null, dto.getIsAsc1 (), WorkerCertificationAudit::getCreateTime)
        );
        return PageUtils.toPage (workerCertificationAuditPage, WorkerCertificationAuditResDTO.class);
    }

    @Autowired
    private ServeProviderMapper  serveProviderMapper;
    @Override
    @Transactional
    public void audit (Long id, String rejectReason, Integer certificationStatus) {
        WorkerCertificationAudit workerCertificationAudit = workerCertificationAuditMapper.selectById (id);
        if (workerCertificationAudit == null) {
            throw new RuntimeException ("审核信息不存在");
        }
        Long serveProviderId = workerCertificationAudit.getServeProviderId ();
        WorkerCertification workerCertification = workerCertificationMapper.selectById (serveProviderId);
        if (workerCertification == null) {
            throw new RuntimeException ("认证信息不存在");
        }
        workerCertification.setCertificationStatus (certificationStatus);
        workerCertification.setCertificationTime (LocalDateTime.now ());
        int i1 = workerCertificationMapper.updateById (workerCertification);
        if (i1 <= 0) {
            throw new RuntimeException ("审核失败");
        }
        workerCertificationAudit.setCertificationStatus (certificationStatus);
        workerCertificationAudit.setRejectReason (rejectReason);
        workerCertificationAudit.setAuditStatus (EnableStatusEnum.ENABLE.getStatus ());
        workerCertificationAudit.setAuditorId (UserContext.currentUserId ());
        workerCertificationAudit.setAuditorName (UserContext.currentUser ().getName ());
        workerCertificationAudit.setAuditTime (LocalDateTime.now ());
        ServeProvider serveProvider = serveProviderMapper.selectById (serveProviderId);
        if (serveProvider == null) {
            throw new RuntimeException ("服务商不存在");
        }
        serveProvider.setName (workerCertificationAudit.getName ());
        serveProviderMapper.updateById (serveProvider);
        int i = workerCertificationAuditMapper.updateById (workerCertificationAudit);

        if (i <= 0) {
            throw new RuntimeException ("审核失败");
        }
    }

    @Override
    public String rejectReason () {
        return this.getById (UserContext.currentUserId ()).getRejectReason ();
    }
}
